#!/bin/bash

# Usage
function Usage(){
	clear
	if [ $# -ne 2 ];then
		echo "Usage: bash -x pkglist_file_name"
		exit 1
	fi
}

# download binaries file
function download_binary(){
	pkglist=$1
	init_path=`pwd`
	rm -rf ${init_path}/check_result
	mkdir ${init_path}/all_binary
	echo "Download binary file is doing, please wait..."
	for pkg in `cat ${pkglist}`
	do	
		osc getbinaries openEuler:Mainline ${pkg} standard_aarch64 aarch64 &>/dev/null
		#osc getbinaries ${proj} ${pkg} standard_x86_64 x86_64 &>/dev/null
		if [ $? -ne 0 ];then
			osc getbinaries openEuler:Factory ${pkg} standard_aarch64 aarch64 &>/dev/null
			if [ $? -ne 0 ];then
				echo "Download ${pkg} binary file failed on obs server !!!"
				echo "Download ${pkg} binary file failed on obs server !!!" >> download_binary_failed
			else
				if [[ "x`ls ${init_path}/binaries/*.rpm 2>/dev/null`" != "x" ]];then
					mv binaries ${pkg} && rm ${pkg}/_* -f && mv ${pkg} all_binary/
					echo "Download ${pkg} binary file succeed !!!"
				else
					echo "Download ${pkg} binary file failed, obs server no binary !!!"
					echo "Download ${pkg} binary file failed, obs server no binary !!!" >> download_binary_failed
				fi
			fi
		else
			if [[ "x`ls ${init_path}/binaries/*.rpm 2>/dev/null`" != "x" ]];then
				mv binaries ${pkg} && rm ${pkg}/_* -f && mv ${pkg} all_binary/
				echo "Download ${pkg} binary file succeed !!!"
			else
				echo "Download ${pkg} binary file failed, obs server no binary !!!"
				echo "Download ${pkg} binary file failed, obs server no binary !!!" >> download_binary_failed
			fi
		fi
	done
	echo "Download all binary files have done!"
	cd ${init_path}/all_binary && ls > ${init_path}/need_check_list
	if [ ! -s ${init_path}/need_check_list ];then
		echo "All pkg have no binaries on obs server, do not need check !!!"
		rm -rf ${init_path}/need_check_list ${init_path}/all_binary ${init_path}/binaries ${init_path}/download_binary_failed 
		exit 1
	fi
	cd ${init_path}
}

# create docker env
function create_env(){
	echo "Creating docker env, please wait..."
	wget https://repo.openeuler.org/openEuler-20.03-LTS/docker_img/aarch64/openEuler-docker.aarch64.tar.xz
	docker load < openEuler-docker.aarch64.tar.xz
	rm openEuler-docker.aarch64.tar.xz -f
	docker stop test_install &>/dev/null
	docker rm test_install &>/dev/null
	docker run -itd --name=test_install openeuler-20.03-lts:latest /bin/bash &>/dev/null
	docker cp ${init_path}/all_binary test_install:/root/
	docker cp ${init_path}/need_check_list test_install:/root/
	docker cp ${init_path}/check_pkg_install_uninstall_indocker.sh test_install:/root/
	docker exec -it test_install /bin/bash -c 'echo "export TMOUT=0" >> /etc/bashrc && source /etc/bashrc 2>/dev/null && exit'
	docker restart test_install &>/dev/null
	echo "Create docker env completed !!!"
}

# start check
function main(){
	docker exec -it test_install /bin/bash -c "cd /root/ && bash check_pkg_install_uninstall_indocker.sh need_check_list"
	docker cp test_install:/root/check_result ./
	rm -rf ${init_path}/need_check_list
	mv ${init_path}/download_binary_failed ${init_path}/check_result/ &>/dev/null
	mv ${init_path}/all_binary ${init_path}/check_result/
	docker stop test_install &>/dev/null
	docker rm test_install &>/dev/null
	echo "Clear docker env complete !!!"
	echo "Checking install and uninstall is complete, you can see log file in directory: ${init_path}/check_result, please backup check_result dir !!!"
}
#init_path=`pwd`
echo "=============================================Begin============================================="
#Usage $@
download_binary $1
create_env
main
echo "=============================================End==============================================="
